package com.charger.PromoteOperations.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.charger.PromoteOperations.domain.ActivityChargingstation;
import com.charger.PromoteOperations.service.ActivityChargingstationService;
import com.charger.chargingStation.domain.Chargingstation;
import com.charger.chargingStation.service.impl.ChargingstationServiceImpl;
import com.charger.giftcert.domain.Giftcertificate;
import com.charger.giftcert.service.IGiftcertificateService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.PromoteOperations.domain.Activity;
import com.charger.PromoteOperations.service.IActivityService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 活动管理Controller
 * 
 * @author ruoyi
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/activity/activity")
public class ActivityController extends BaseController
{
    @Autowired
    private IActivityService activityService;
    @Autowired
    private IGiftcertificateService giftcertificateService;
    @Autowired
    private ActivityChargingstationService activityChargingstationService;
    @Autowired
    private ChargingstationServiceImpl chargingstationService;

    /**
     * 查询活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('activity:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(Activity activity)
    {
        startPage();
        List<Activity> list = activityService.selectActivityList(activity);
        for (Activity activityList : list){
            Long giftcertificateId = activityList.getGiftcertificateId();
            Giftcertificate byId = giftcertificateService.selectGiftcertificateById(giftcertificateId);
            activityList.setGiftcertificate(byId);
            Long id = activityList.getId();
            QueryWrapper<ActivityChargingstation> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("activity_id",id);
            List<ActivityChargingstation> midList = activityChargingstationService.list(wrapper1);
            List<Chargingstation> chargingstationList = new ArrayList();
            for (ActivityChargingstation activityChargingstation : midList){
                Long chargingstationId = activityChargingstation.getChargingstationId();
                Chargingstation byChargingstationId = chargingstationService.selectChargingstationByChargingstationId(chargingstationId);
                chargingstationList.add(byChargingstationId);
            }
            activityList.setChargingstations(chargingstationList);
        }
        return getDataTable(list);
    }

    /**
     * 导出活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('activity:activity:export')")
    @Log(title = "活动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Activity activity)
    {
        List<Activity> list = activityService.selectActivityList(activity);
        ExcelUtil<Activity> util = new ExcelUtil<Activity>(Activity.class);
        util.exportExcel(response, list, "活动管理数据");
    }

    /**
     * 获取活动管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(activityService.selectActivityById(id));
    }

    /**
     * 新增活动管理
     */
    @PreAuthorize("@ss.hasPermi('activity:activity:add')")
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Activity activity)
    {
        int i = activityService.insertActivity(activity);
        List<Long> chargerStationId1 = activity.getChargerStationId();
        if (chargerStationId1 != null){
            Long id = activity.getId();
            for (Long chargerStationId2 : chargerStationId1){
                ActivityChargingstation activityChargingstation = new ActivityChargingstation();
                activityChargingstation.setActivityId(id);
                activityChargingstation.setChargingstationId(chargerStationId2);
                activityChargingstationService.save(activityChargingstation);
            }
        }
        return toAjax(i);
    }

    /**
     * 修改活动管理
     */
    @PreAuthorize("@ss.hasPermi('activity:activity:edit')")
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Activity activity)
    {
//        先清除活动管理充电站中间表数据
        Long id = activity.getId();
        boolean b = activityChargingstationService.removeById(id);
//        再添加活动管理充电站中间表数据
        List<Long> chargerStationId1 = activity.getChargerStationId();
        if (chargerStationId1 != null){
            for (Long chargerStationId2 : chargerStationId1){
                ActivityChargingstation activityChargingstation = new ActivityChargingstation();
                activityChargingstation.setActivityId(id);
                activityChargingstation.setChargingstationId(chargerStationId2);
                activityChargingstationService.save(activityChargingstation);
            }
        }
        return toAjax(activityService.updateActivity(activity));
    }

    /**
     * 删除活动管理
     */
    @PreAuthorize("@ss.hasPermi('activity:activity:remove')")
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(activityService.deleteActivityByIds(ids));
    }
}
